package com.ruoyi.cs.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cs.domain.TBox;
import com.ruoyi.cs.domain.TWarGrate;
import com.ruoyi.cs.domain.TWarUser;
import com.ruoyi.cs.domain.vo.TWarUserParamVo;
import com.ruoyi.cs.mapper.TBoxMapper;
import com.ruoyi.cs.mapper.TWarGrateMapper;
import com.ruoyi.cs.mapper.TWarUserMapper;
import com.ruoyi.cs.service.ITWarGrateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 战令等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-11
 */
@Service
@SuppressWarnings("all")
public class TWarGrateServiceImpl implements ITWarGrateService
{
    @Autowired
    private TWarGrateMapper tWarGrateMapper;
    @Autowired
    private TBoxMapper tBoxMapper;
    @Autowired
    private TWarUserMapper tWarUserMapper;

    /**
     * 查询战令等级
     * 
     * @param id 战令等级主键
     * @return 战令等级
     */
    @Override
    public TWarGrate selectTWarGrateById(Long id)
    {
        return tWarGrateMapper.selectTWarGrateById(id);
    }

    /**
     * 查询战令等级列表
     * 
     * @param tWarGrate 战令等级
     * @return 战令等级
     */
    @Override
    public List<TWarGrate> selectTWarGrateList(TWarGrate tWarGrate)
    {
        List<TWarGrate> list = tWarGrateMapper.selectTWarGrateList(tWarGrate);
        if(CollectionUtil.isNotEmpty(list)){
            /**设置奖励内容*/
            List<String> boxIds = list.stream().filter(bean -> bean.getWarType() == 1).map(TWarGrate::getWarBox).distinct().map(String::valueOf).collect(Collectors.toList());
            List<TBox> tBoxList = tBoxMapper.getTBoxListBatch(null,boxIds);
            list.forEach(bean -> {
                if(bean.getWarType() == 0){
                    bean.setContent(bean.getWarFee());
                }else if(bean.getWarType() == 1){
                    Optional<TBox> optionalTBox = tBoxList.stream().filter(box -> box.getBoxId().equals(bean.getWarBox().toString())).findFirst();
                    if(optionalTBox.isPresent()){
                        bean.setContent(optionalTBox.get().getBoxName());
                    }else {
                        bean.setContent("此箱子不存在");
                    }
                }
            });
            /**设置达标人数*/
            List<TWarUser> warUserList = tWarUserMapper.warUserList(tWarGrate.getWarId());
            list.forEach(bean -> {
                Long count = warUserList.stream().filter(user -> user.getWarGrate() >= bean.getGrate()).count();
                bean.setComplianceUserNumber(count);
            });
        }
        return list;
    }

    /**
     * 新增战令等级
     * 
     * @param tWarGrate 战令等级
     * @return 结果
     */
    @Override
    public int insertTWarGrate(TWarGrate tWarGrate)
    {
        tWarGrate.setCreateTime(DateUtils.getNowDate());
        Integer grate = tWarGrateMapper.maxGrate(tWarGrate.getWarId());
        tWarGrate.setGrate(grate+1);
        return tWarGrateMapper.insertTWarGrate(tWarGrate);
    }

    /**
     * 修改战令等级
     * 
     * @param tWarGrate 战令等级
     * @return 结果
     */
    @Override
    public int updateTWarGrate(TWarGrate tWarGrate)
    {
        tWarGrate.setUpdateTime(DateUtils.getNowDate());
        return tWarGrateMapper.updateTWarGrate(tWarGrate);
    }

    /**
     * 批量删除战令等级
     * 
     * @param ids 需要删除的战令等级主键
     * @return 结果
     */
    @Override
    public int deleteTWarGrateByIds(Long[] ids)
    {
        return tWarGrateMapper.deleteTWarGrateByIds(ids);
    }

    /**
     * 删除战令等级信息
     * 
     * @param id 战令等级主键
     * @return 结果
     */
    @Override
    public int deleteTWarGrateById(Long id)
    {
        return tWarGrateMapper.deleteTWarGrateById(id);
    }

    @Override
    public List<TWarUser> warUserList(TWarUserParamVo tWarUserParamVo) {
        return tWarUserMapper.warUserListByWarId(tWarUserParamVo);
    }

    /**
     * @Desc: 获取战令箱子列表
     * @author: zz
     * @date: 2023/12/14
     */
    @Override
    public List<Map<String, Object>> getWarBoxList(String searchValue) {
        return tBoxMapper.getWarBoxList(searchValue);
    }
}
